# Maintainer: Ray Donnelly <mingw.android@gmail.com>
# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=ag
_longname=the_silver_searcher
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.31.0.r1666.0e577cc
pkgrel=1
pkgdesc="The Silver Searcher: An attempt to make something better than ack, which itself is better than grep (mingw-w64)"
arch=('any')
url="http://geoff.greer.fm/ag"
license=("Apache")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-pcre"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs') # '!strip' 'debug')
source=("${_realname}"::"git+https://github.com/ggreer/the_silver_searcher.git"
        "0001-lang-Allow-both-extensions-and-full-filenames.patch"
        "0002-options-Fix-ordering-problems-with-color.patch"
        "0003-lang-Add-autotools-ac-am-in-m4-pc.patch"
        "0004-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch"
        "0005-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch"
        "0006-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch")
sha256sums=('SKIP'
            'eface051f399e098ffe5719d9d65334ac62cf079385cd7ff179203e0184709a9'
            '2b6ca4627ce76ae41ac9873357257e8236955445af181011f051027a7260077d'
            '6a6e5d555b15daed84cf523699aef7dd1b8fd0ff499ea961645640298e4de202'
            'a203c2bcdc24461e188fec14f240674d2e5614f2ee7f65a210bc03973786094f'
            'abe95b36af951d5515f58c04de847fd4c68b7b2e38a83da3f9cf20792229ad78'
            '794db6b04ff5656d3e93bc4f7323fbdbbcdb9a1f1a31b9a2956b56660c7de19d')

pkgver() {
  cd ${srcdir}/${_realname}
  local AC_INIT_VER=$(grep AC_INIT configure.ac -A5 | tr '\n' ' ' | sed -e 's/AC_INIT([^,]\+,\s*\[\?\([0-9.a-z]\+\).*/\1/')
  printf "%s.r%s.%s" "${AC_INIT_VER}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}"/${_realname}
  git am "${srcdir}"/0001-lang-Allow-both-extensions-and-full-filenames.patch
  git am "${srcdir}"/0002-options-Fix-ordering-problems-with-color.patch
  git am "${srcdir}"/0003-lang-Add-autotools-ac-am-in-m4-pc.patch
  git am "${srcdir}"/0004-lang-Add-to-make-Makefile-Makefile.Debug-Makefile.Re.patch
  git am "${srcdir}"/0005-lang-Add-makepkg-PKGBUILD-diff-patch-in-install.patch
  git am "${srcdir}"/0006-lang-Add-cmake-CMakeLists.txt-CMakeCache.txt.patch

  # configure.ac forces -O2, so force it to -O0 if debugging.
  if check_option "debug" "y"; then
    sed -i "s#-O2#-O0#g" configure.ac
  fi

  autoreconf -fi
}

build() {

  mkdir -p "${srcdir}/build-${CARCH}"
  cd "${srcdir}/build-${CARCH}"

  "${srcdir}"/${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --{build,host}=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make DESTDIR="${pkgdir}" install
}
